Shell编程规范:Don't use ls | 您所在的位置:网站首页 › shell 编程规范 › Shell编程规范:Don't use ls |
一、背景 最近在写一个私有化部署脚本的时候频繁的使用了ls和grep的组合,但是插件ShellCheck却一直在给我标黄,我这该死的洁癖终于在今天受不了了,这个使用了这么多年的好搭档怎么就那么不讨喜呢! 很明显,提示告诉我们不要使用ls|grep的搭配,建议我们使用一个通配符或者带有条件的循环。。。 一个ls都有这么多讲究,我们来看个wiki的使用反例: $ touch 'a space' $'a\nnewline' $ echo "don't taze me, bro" > a $ ls | cat a a newline a spacels后面用管道符输出的结果怎么和预想的不一样??,我们来看看文件列表: $ ls -l total 8 -rw-r----- 1 lhunath lhunath 19 Mar 27 10:47 a -rw-r----- 1 lhunath lhunath 0 Mar 27 10:47 a?newline -rw-r----- 1 lhunath lhunath 0 Mar 27 10:47 a spaceLinux中的文件名是个神奇的存在,你可以使用几乎任意字符:空格、换行符、逗号、管道符等等。 默认模式下,如果输出不是终端的话,ls会用换行符分割开文件名,所以问题就来了:从ls的输出中,您或计算机都无法知道它的哪些部分构成了文件名。是每个字吗?不。是每行吗?不。这个问题没有正确答案,只有:你不知道。 哈哈哈,惊不惊喜?意不意外?当然,平时的使用其实都没有问题的,只是插件不建议我们这么用,那我就是改改我的使用吧,洁癖太可怕了。。。 三、修改写法1、ls | grep file这个其实挺好改的,我的本意是想看下prometheus的目录权限、grafana目录下grafana.db文件的权限 查一个目录的权限可以修改为(d指定只看目录): ➜ service-monitor git:(master) ✗ ls -dl prometheus drwxr-xr-x 17 Charles staff 544 8 26 21:09 prometheus由于ls本身就有检索的功能,查看文件的权限就很简单了 ➜ service-monitor git:(master) ✗ ls -l grafana/grafana.db -rw-rw-rw- 1 Charles staff 2232320 8 26 22:36 grafana/grafana.db2、ls | grep regexp简化一下原有语句: for tarfile in $(ls images | grep '.tar$'); do echo "loading $tarfile"; done本意是想加载images文件夹下所有tar文件,先打印,后load镜像。 我们知道ls后面的查询只能跟通配符,不能跟正则表达式,所以更改如下: ➜ build git:(master) ✗ ls images | grep '.tar$' monitor.tar ➜ build git:(master) ✗ ls images/*.tar images/monitor.tar然后for循环可修改为: ➜ build git:(master) ✗ file=$(ls images | grep '.tar$') ➜ build git:(master) ✗ echo $file monitor.tar ➜ build git:(master) ✗ file=images/*.tar ➜ build git:(master) ✗ echo $file images/*.tar ➜ build git:(master) ✗ for tarfile in images/*tar; do echo "loading $tarfile"; done loading images/monitor.tar3、ls | grep -v multi这个语句的本意是查询出当前目录下所有不是.sql或.zip结尾的文件由于ls不能用正则,也不能用反选,这个语句的修改一开始让我还很头疼,后来发现有个shell操作选项shopt,这个工具可以激活或关闭指定的shell行为选项。 比如我现在要打开extglob模式,该模式可以给ls扩展匹配操作符,能使文件匹配更加方便. 不然不识别! 开启命令: shopt -s extglob关闭命令: shopt -u extglob5个模式匹配操作符 ?(pattern-list) - 所给模式匹配0次或1次;*(pattern-list) - 所给模式匹配0次以上包括0次;+(pattern-list) - 所给模式匹配1次以上包括1次;@(pattern-list) - 所给模式仅仅匹配1次;!(pattern-list) - 不匹配括号内的所给模式。#反选删除文件: #(打开extglob模式) shopt -s extglob rm -fr !(file1) #多个要排除的: rm -rf !(file1|file2)所以这时候我们的修改就很简单了 shopt -s extglob rm -rf !(*.zip|*.sql) && ls -l四、最后改完之后心情豁然开朗,很多时候规范不只是有束缚编码行为的好处,还可以使你对代码使用有着更深的理解。 五、参考ShellCheck Wiki: Don't use ls | grepShellCheck Wiki: Iterating over ls output is fragile. Use globs.Why you shouldn't parse the output of lsPattern Matching In Bashshoptlinux extglob模式 和rm反选 |
CopyRight 2018-2019 实验室设备网 版权所有 |